﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using Zvezda.FSO2.DB;
using System.Xml;
using Telerik.WinControls.UI;

namespace FSO.Reports
{
    /// <summary>
    /// Компонент для отображения параметров отчета "Неоплаченые входящие счета"
    /// </summary>
    public partial class UserControlNotPayedInputInvoicesParams : UserControl, IReportParamList
    {
        public UserControlNotPayedInputInvoicesParams()
        {
            InitializeComponent();
        }

        #region IReportParamList Members

        public Microsoft.Reporting.WinForms.ReportParameter[] Parameters
        {

            get
            {
                List<ReportParameter> _listParams = new List<ReportParameter>(7);

                // Дата с
                //ReportParameter paramDateFrom = new ReportParameter("p_date_from", radDateTimePickerFrom.Value.ToShortDateString(), false);
                //_listParams.Add(paramDateFrom);

                //// Дата по
                //ReportParameter paramDateTo = new ReportParameter("p_date_to", radDateTimePickerTo.Value.ToShortDateString(), false);
                //_listParams.Add(paramDateTo);

                // Органиация
                string sNsiOrganization = null;
                if (!userControlOrganizationSelector1.IsNull)
                {
                    sNsiOrganization = userControlOrganizationSelector1.SelectedRow.ID.ToString();
                }
                _listParams.Add(new ReportParameter("p_nsi_organization_id", sNsiOrganization, false));

                // Материалы
                //string sMaterials = null;
                //if (radListBoxMaterials.Items.Count > radListBoxMaterials.SelectedItems.Count)
                //{
                //    XmlDocument xmlMaterials = new XmlDocument();
                //    XmlNode nodeMaterials = xmlMaterials.CreateNode(XmlNodeType.Element, "materials", "");
                //    xmlMaterials.AppendChild(nodeMaterials);
                //    foreach (RadListDataItem item in radListBoxMaterials.SelectedItems)
                //    {
                //        XmlNode nodeMaterial = xmlMaterials.CreateNode(XmlNodeType.Element, "material", "");
                //        XmlAttribute attID = xmlMaterials.CreateAttribute("id");
                //        attID.Value = item.Value.ToString();
                //        nodeMaterial.Attributes.Append(attID);
                //        nodeMaterials.AppendChild(nodeMaterial);
                //    }

                //    sMaterials =  xmlMaterials.InnerXml;
                //}
                //ReportParameter paramMaterials = new ReportParameter("p_xml_materials", sMaterials, false);
                //_listParams.Add(paramMaterials);

                // Договор
                string sContractValue = null;
                string sContractNumberCustomer = null;
                if (!userControlContractSelector1.IsNull)
                {
                    sContractValue = userControlContractSelector1.SelectedRow.ID.ToString();
                    sContractNumberCustomer = userControlContractSelector1.SelectedRow.NUMBER.ToString();
                }
                _listParams.Add(new ReportParameter("p_contract_id", sContractValue, false));
                _listParams.Add(new ReportParameter("v_contract_number", sContractNumberCustomer, false));

				//порог остатка
				if (radTextBoxBalanceThreshold.Text.Length > 0)
				{
					float threshold = 0.0F;
					if (float.TryParse(radTextBoxBalanceThreshold.Text, out threshold))
						_listParams.Add(new ReportParameter("p_balance_threshold", threshold.ToString(), false));
					else
						_listParams.Add(new ReportParameter("p_balance_threshold", ((float)0).ToString(), false));
				}
				else
					_listParams.Add(new ReportParameter("p_balance_threshold", ((float)0).ToString(), false));

                // Направление
                string sTypeOfActivity = null;
                string sTypeOfActivityName = null;
                if (!userControlTypeOfActivitySelector1.IsNull)
                {
                    sTypeOfActivity = userControlTypeOfActivitySelector1.SelectedValue;
                    sTypeOfActivityName = userControlTypeOfActivitySelector1.SelectedText;
                }
                _listParams.Add(new ReportParameter("p_nsi_type_of_activity_id", sTypeOfActivity, false));
                _listParams.Add(new ReportParameter("v_nsi_type_of_activity", sTypeOfActivityName, false));

                // № папки
                string sFolderNum = null;
                string sFolderNumName = null;
                if (radCheckBoxIsFolderNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
                {
                    sFolderNum = radDropDownListFolder.SelectedItem.Text;
                    sFolderNumName = radDropDownListFolder.SelectedItem.Text;
                }

                _listParams.Add(new ReportParameter("p_folder_num", sFolderNum, false));
                _listParams.Add(new ReportParameter("v_folder_num_name", sFolderNumName, false));

                //№ контракта
                string sContractNumber = null;
                if (radTextBoxContractNum.Text != null && radTextBoxContractNum.Text.Trim() != "")
                {
                    sContractNumber = radTextBoxContractNum.Text.Trim();
                }
                _listParams.Add(new ReportParameter("p_contract_number", sContractNumber, false));

                // Указание к оплате
                string sPayDirectValue = null;
                if (radCheckBoxIsPayDirectNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
                {
                    sPayDirectValue = radDropDownListPayDirect.SelectedItem.Value.ToString();
                }
                _listParams.Add(new ReportParameter("p_nsi_pay_direct_id", sPayDirectValue, false));

                string sPlanPayDate = null;
                if (radDateTimePickerPlanDay.Checked)
                {
                    sPlanPayDate = radDateTimePickerPlanDay.Value.ToShortDateString();
                }
                _listParams.Add(new ReportParameter("p_plan_payment_date", sPlanPayDate, false));
                //источник финансирования
                string sListFinResourceValue = null;
                if (radCheckBoxIsListFinNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
                {
                    sListFinResourceValue = radDropDownListFinResource.SelectedItem.Value.ToString();
                }
                _listParams.Add(new ReportParameter("p_nsi_fin_res_id", sListFinResourceValue, false));
                //статья расходов
                string sListNsiExpenseItemValue = null;
                if (radCheckBoxIsExpItemNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
                {
                    sListNsiExpenseItemValue = radDropDownListNsiExpenseItem.SelectedItem.Value.ToString();
                }
                _listParams.Add(new ReportParameter("p_nsi_expense_item_id", sListNsiExpenseItemValue, false));

                return _listParams.ToArray();
            }
        }

        #endregion
        /// <summary>
        /// Загружает данные из БД
        /// </summary>
        private void UserControlPayedInputInvoicesParams_Load(object sender, EventArgs e)
        {
            tableAdapterManager1.NSI_PAY_DIRECTTableAdapter.Fill(dataSetFSO1.NSI_PAY_DIRECT);
            tableAdapterManager1.NSI_FINANCE_RESOURCETableAdapter.Fill(dataSetFSO1.NSI_FINANCE_RESOURCE);
            tableAdapterManager1.NSI_EXPENSE_ITEMTableAdapter.Fill(dataSetFSO1.NSI_EXPENSE_ITEM);
        }

        private void radCheckBoxIsFolderNull_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radDropDownListFolder.Enabled = true;
            }
            else
            {
                radDropDownListFolder.Enabled = false;
            }
        }

        private void radCheckBoxIsPayDirectNull_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
             if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radDropDownListPayDirect.Enabled = true;
            }
            else
            {
                radDropDownListPayDirect.Enabled = false;
            }
        }

        private void radCheckBoxIsListFinNull_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radDropDownListFinResource.Enabled = true;
            }
            else
            {
                radDropDownListFinResource.Enabled = false;
            }
        }

        private void radCheckBoxIsExpItemNull_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radDropDownListNsiExpenseItem.Enabled = true;
            }
            else
            {
                radDropDownListNsiExpenseItem.Enabled = false;
            }
        }
    }
}
